﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Windows.Forms;
using QuanLyBanHang.DataConnection;

namespace QuanLyBanHang.QuerySQL
{
    class ClassqlDathang
    {
        Connect conn = new Connect();
        //show don dat hang
        public DataTable ShowDH()
        {
            DataTable tb = new DataTable(); ;
            try
            {
                string sql = "select MADH,HOTEN,TENNV,NGAYDATHANG,NGAYGIAOHANG,TONGGIA_DH from DATHANG,KHACHHANG,NHANVIEN where DATHANG.MAKH = KHACHHANG.MAKH and DATHANG.MANV = NHANVIEN.MANV";
                tb = conn.getTable(sql);
            }
            catch (Exception e)
            {
                MessageBox.Show("Lỗi: " + e.Message);
            }
            return tb;
        }
        //
        public DataTable ShowCTHD(int i, string madh = null)
        {
            DataTable tb = new DataTable();
            switch (i)
            {
                case 0:
                    {
                        try
                        {
                            string sql = "select *from CHITIET_PDH";
                            tb = conn.getTable(sql);
                        }
                        catch (Exception e)
                        {
                            MessageBox.Show("Lỗi: " + e.Message);
                        }
                    }
                    break;
                case 1:
                    {

                        try
                        {
                            string sql = "select *from CHITIET_PDH where MADH like '%" + madh + "%'";
                            tb = conn.getTable(sql);
                        }
                        catch (Exception e)
                        {
                            MessageBox.Show("Lỗi: " + e.Message);
                        }
                        break;
                    }
            }
            return tb;
        }
        // tìm kiếm
        public DataTable TKHD(int i, string madh, string tenkh = null)
        {
            DataTable dt = new DataTable();
            switch (i)
            {
                case 0:
                    {
                        try
                        {
                            String sql = "select MADH,HOTEN,TENNV,NGAYDATHANG,NGAYGIAOHANG,TONGGIA_DH from DATHANG,KHACHHANG,NHANVIEN where MADH like '%"+madh+"%' and DATHANG.MAKH = KHACHHANG.MAKH and DATHANG.MANV = NHANVIEN.MANV";
                            dt = conn.getTable(sql);
                        }
                        catch (Exception e)
                        {
                            MessageBox.Show("Lỗi: " + e.Message);
                        }
                        break;
                    }
                case 1:
                    {
                        try
                        {
                            String sql = "select MADH,HOTEN,TENNV,NGAYDATHANG,NGAYGIAOHANG,TONGGIA_DH from DATHANG,KHACHHANG,NHANVIEN where HOTEN like N'%" + tenkh + "%' and DATHANG.MAKH = KHACHHANG.MAKH and DATHANG.MANV = NHANVIEN.MANV";
                            dt = conn.getTable(sql);
                        }
                        catch (Exception e)
                        {
                            MessageBox.Show("Lỗi: " + e.Message);
                        }
                        break;
                    }
            }
            return dt;
        }
        //
        public DataTable TKCT(string mahd)
        {
            DataTable tb = new DataTable(); ;
            try
            {
                string sql = "select *from CHITIET_HD where MAHD = '" + mahd + "' ";
                tb = conn.getTable(sql);
            }
            catch (Exception e)
            {
                MessageBox.Show("Lỗi: " + e.Message);
            }
            return tb;
        }
        //xoa
        public void XoaDH(string madh)
        {
            try
            {
                string sql = "delete DATHANG where MADH = '" + madh + "'";
                conn.ExcuteQuerySql(sql);
                MessageBox.Show("Xóa thành công !!!");
            }
            catch (Exception e)
            {
                MessageBox.Show("Lỗi: " + e.Message);
            }
        }
        public void XoaCTDH(string madh)
        {
            try
            {
                string sql = "delete CHITIET_PDH where MADH = '" + madh + "'";
                conn.ExcuteQuerySql(sql);
            }
            catch (Exception e)
            {
                MessageBox.Show("Lỗi: " + e.Message);
            }
        }
    }
}
